#encoding:utf-8
from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register
from common.models import Industry
from countries_states.models import Country, State, Municipal
from models import Company_Industry, Company_Area
from django.utils.translation import ugettext_lazy as _

@dajaxice_register
def multiply(request, a, b):
    dajax = Dajax()
    result = int(a) * int(b)
    dajax.assign('#result','value',str(result))
    return dajax.json()

@dajaxice_register
def alert_example(request):
    dajax = Dajax()
    dajax.alert('Hello from python!')
    return dajax.json()

@dajaxice_register
def updatecombo(request, option):
    dajax = Dajax()
    options = [['Madrid', 'Barcelona', 'Vitoria', 'Burgos'],
               ['Paris', 'Evreux', 'Le Havre', 'Reims'],
               ['London', 'Birmingham', 'Bristol', 'Cardiff']]
    out = []
    for option in options[int(option)]:
        out.append("<option value='#'>%s</option>" % option)

    dajax.assign('#combo2', 'innerHTML', ''.join(out))
    return dajax.json()

@dajaxice_register
def get_countries(request):
    dajax = Dajax()
    countries = Country.objects.filter(active=True)
    choices=[]
    for country in countries:
        choices.append("<option value='%d'>%s</option>" % (country.id, country.printable_name))
    dajax.assign('#id_country', 'innerHTML', ''.join(choices))
    return dajax.json()

@dajaxice_register
def get_states(request, option):
    dajax = Dajax()
    states = State.objects.filter(country_id=option)
    choices = []
    if len(states) >= 1:
        choices.append("<option value='-1'>Seleccione...</option>")
    else:
        choices.append("<option value='0'>No aplica</option>")
    for state in states:
        # if selected == state.id:
        #     choices.append("<option value='%d' selected='selected'>%s</option>" % (state.id, state.name))
        #     continue
        choices.append("<option value='%d'>%s</option>" % (state.id, state.name))
    dajax.assign('#id_state', 'innerHTML', ''.join(choices))
    return dajax.json()

@dajaxice_register
def get_municipals(request, option):
    dajax = Dajax()
    municipals = Municipal.objects.filter(state_id=option)
    choices = []
    if len(municipals) >= 1:
        choices.append("<option value='-1'>Seleccione...</option>")
    else:
        choices.append("<option value='0'>No aplica</option>")
    for municipal in municipals:
        # if selected == municipal.id:
        #     choices.append("<option value='%d' selected='selected'>%s</option>" % (municipal.id, municipal.name))
        #     continue
        choices.append("<option value='%d'>%s</option>" % (municipal.id, municipal.name))
    dajax.assign('#id_municipal', 'innerHTML', ''.join(choices))
    return dajax.json()

@dajaxice_register
def get_company_industries(request, option):
    dajax = Dajax()
    industries = Company_Industry.objects.filter(company_area_id=option)
    choices = []
    if len(industries) >= 1:
        choices.append("<option value='-1'>Seleccione...</option>")
    else:
        choices.append("<option value='0'>No aplica</option>")
    for industry in industries:
        # if selected == state.id:
        #     choices.append("<option value='%d' selected='selected'>%s</option>" % (state.id, state.name))
        #     continue
        choices.append("<option value='%d'>%s</option>" % (industry.id, industry.name))
    dajax.assign('#id_company_industry', 'innerHTML', ''.join(choices))
    return dajax.json()

@dajaxice_register
def get_industries(request, option):
    dajax = Dajax()
    industries = Industry.objects.filter(area_id=option)
    choices = []
    choices.append("<option value='-1'>Todas</option>")
    for industry in industries:
        # if selected == state.id:
        #     choices.append("<option value='%d' selected='selected'>%s</option>" % (state.id, state.name))
        #     continue
        choices.append("<option value='%d'>%s</option>" % (industry.id, industry.name))
    dajax.assign('#id_industry', 'innerHTML', ''.join(choices))
    return dajax.json()


# @dajaxice_register
# def get_municipals(request, option):
#     dajax = Dajax()
#     municipals = Municipal.objects.filter(state_id=option)
#     choices = []
#     remove_class = ''
#     add_class = ''
#     if len(municipals) >= 1:
#         choices.append("<option value='-1'>Seleccione...</option>")
#         remove_class = 'invisible'
#         add_class = 'visible'
#     else:
#         choices.append("<option value='0'>No aplica</option>")
#         remove_class = 'visible'
#         add_class = 'invisible'
#     for municipal in municipals:
#         # if selected == municipal.id:
#         #     choices.append("<option value='%d' selected='selected'>%s</option>" % (municipal.id, municipal.name))
#         #     continue
#         choices.append("<option value='%d'>%s</option>" % (municipal.id, municipal.name))
#     dajax.assign('#id_municipal', 'innerHTML', ''.join(choices))
#     dajax.remove_css_class('#div_municipal', remove_class)
#     dajax.add_css_class('#div_municipal', add_class)
#     return dajax.json()